import { createRouter, createWebHistory } from 'vue-router';
import { useUserStore } from '@/stores'; 
import parseJWT from '@/utils/jwt';


const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/teacher',
    name: 'TeacherDashboard',
    component: () => import('../views/TeacherLayout.vue'),
    redirect: '/teacher/home',
    meta: { requiresAuth: true, role: ['1'] },
    children: [
      {
        path: 'home',
        component: () => import('../components/teacher/index.vue'),
        meta: { requiresAuth: true, role: ['1'] }
      },
      {
        path: 'assign-homework',
        name: 'AssignHomework',
        component: () => import('../components/teacher/AssignHomework.vue'),
        meta: { requiresAuth: true, role: ['1'] }
      },
      {
        path: 'grade-homework',
        name: 'GradeHomework',
        component: () => import('../components/teacher/GradeHomework.vue'),
        meta: { requiresAuth: true, role: ['1'] }
      }
    ]
  },
  {
    path: '/student',
    name: 'StudentDashboard',
    component: () => import('../views/StudentLayout.vue'),
    redirect: '/student/home',
    meta: { requiresAuth: true, role: ['0'] },
    children: [
      {
        path: 'home',
        name: 'StudentHome',
        component: () => import('../components/student/index.vue'),
        meta: { requiresAuth: true, role: ['0'] }
      },
      {
        path: 'submit-homework',
        name: 'SubmitHomework',
        component: () => import('../components/student/SubmitHomework.vue'),
        meta: { requiresAuth: true, role: ['0'] }
      },
      {
        path: 'join-class',
        name: 'JoinClass',
        component: () => import('../components/student/JoinClass.vue'),
        meta: { requiresAuth: true, role: ['0'] }
      },
      {
        path: 'view-feedback',
        name: 'ViewFeedback',
        component: () => import('../components/student/ViewFeedback.vue'),
        meta: { requiresAuth: true, role: ['0'] }
      }
    ]
  },
  {
    path: '/admin',
    name: 'AdminDashboard',
    component: () => import('../views/AdminLayout.vue'),
    redirect: '/admin/home',
    meta: { requiresAuth: true, role: ['2'] },
    children: [
      {
        path: 'home',
        name: 'AdminHome',
        component: () => import('../components/admin/index.vue'),
        meta: { requiresAuth: true, role: ['2'] }
      },
      {
        path: 'student-management',
        name: 'StudentManagement',
        component: () => import('../components/admin/StudentManagement.vue'),
        meta: { requiresAuth: true, role: ['2'] }
      },
      {
        path: 'teacher-management',
        name: 'TeacherManagement',
        component: () => import('../components/admin/TeacherManagement.vue'),
        meta: { requiresAuth: true, role: ['2'] }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/404.vue'),
    redirect: '/login'
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

const whiteList = ['/login', '/register'];

router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  const token = userStore.token;

  // 先判断是否在白名单里，是就放行
  if (whiteList.includes(to.path)) {
    return next();
  }

  // token 不存在，直接跳登录页
  if (!token) {
    return next('/login');
  }

  // 解析 token
  const payload = parseJWT(token);

  // 判断 token 是否过期
  // JWT 的 payload 一般有 exp 字段，单位是秒，代表过期时间戳
  const now = Math.floor(Date.now() / 1000);
  if (!payload || !payload.exp || payload.exp < now) {
    // token 过期，跳登录页
    userStore.reset();  // 这里假设有清理 token 的方法
    return next('/login');
  }

  // token 存在且未过期，放行
  next();
});


export default router;
